//
// Created by zx208 on 2024/10/27.
//

#include "LinkedStack.h"

Status Init_LS(LinkedStack *S){
    *S = NULL;
    return OK;
}

Status Push_LS(LinkedStack *S, ElemType e){
    LinkedStack p = (LinkedStack) malloc(sizeof( StackNode));
    p->data = e;
    p->next = *S; // 将新结点插入栈顶
    *S = p; // 修改栈顶指针
    return OK;
}

Status Pop_LS(LinkedStack *S, ElemType *e){
    LinkedStack pfree = *S;
    *e = (*S)->data;
    *S = (*S)->next; // 修改栈顶指针
    free(pfree);
    return OK;
}

ElemType GetTop(LinkedStack *S) {
    if (*S) return (*S)->data;
    else exit(OVERFLOW);
}

Status IsEmpty(LinkedStack *S) {
    if (!(*S)) return TRUE;
    else return FALSE;
}

int main(void){
    LinkedStack S;
    Init_LS(&S);
    Push_LS(&S, 1);
    Push_LS(&S, 2);
    Push_LS(&S, 3);
    Push_LS(&S, 4);
    Push_LS(&S, 5);
    while (!IsEmpty(&S))
    {
        ElemType e;
        Pop_LS(&S, &e);
        printf("%d ", e);
    }
    return 0;
}